<?php

namespace app\api\controller\kami;

use app\common\controller\ApiAes;
use think\Config;

/**
 * 会员接口
 */
class User extends ApiAes
{
    protected $noNeedLogin = ['card_login'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }
    }

    /**
     * 会员登录
     * @ApiMethod (POST)
     * @ApiParams (name="card_no", type="string", required=true, description="卡密")
     * @ApiParams (name="bind_mac", type="string", required=true, description="mac地址")
     */
    public function card_login()
    {
        $card_no = $this->request->route('card_no');
        $bind_mac = $this->request->route('bind_mac');
        if (!$card_no || !$bind_mac) {
            $this->error(__('Invalid parameters'));
        }
        $ret = $this->auth->card_login($card_no, $bind_mac);
        // 查询配置
        $server_info = \app\common\model\Config::where('name', 'server_info')->find();
        if ($server_info) {
            $server_info = $server_info['value'];
        }
        if (!$ret) {
            $this->error($this->auth->getError());
        }
        $userInfo = [
            'userinfo'    => $this->auth->getUserinfo(),
            'server_info' => $server_info,
        ];
        $this->success(__('Logged in successful'), $userInfo);
        //        $this->success('success', $result);
    }

    public function check_card()
    {
        $card_no = $this->request->param('card_no');
        if (!$card_no) {
            $this->error(__('Invalid parameters'));
        }
        $ret = $this->auth->check_card($card_no);
        if (!$ret) {
            $this->error($this->auth->getError());
        }
        $this->success(__('Card no is available'));
        //        $this->success('success', $result);
    }

    /**
     * 退出登录
     * @ApiMethod (POST)
     */
    public function logout()
    {
        if (!$this->request->isPost()) {
            $this->error(__('Invalid parameters'));
        }
        $this->auth->logout();
        $this->success(__('Logout successful'));
        //        $this->success('success', $result);
    }
}
